package cn.yuan.xiaoyu.newsmodule.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.OnClick;
import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.activity.BaseActivity;
import cn.yuan.xiaoyu.commonmodule.utils.StatusBarCompatUtils;

/**
 * Created by yukuoyuan on 2017/3/8.
 * 这是一个新闻详情的界面
 */

public class NewsDetailsActivity extends BaseActivity {
    @Bind(R.id.wv_news_details)
    WebView wvNewsDetails;
    @Bind(R.id.pb_web_progress)
    ProgressBar pbWebProgress;
    @Bind(R.id.iv_toolbar_left)
    ImageView ivToolbarLeft;
    @Bind(R.id.tv_toolbar_title)
    TextView tvToolbarTitle;

    @Override
    protected void bindPresenter() {

    }

    @Override
    protected void initData(Bundle extras) {

        wvNewsDetails.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (pbWebProgress == null) {
                    return;
                }
                pbWebProgress.setProgress(newProgress);
                if (newProgress == 100) {
                    pbWebProgress.setVisibility(View.INVISIBLE);
                } else {
                    pbWebProgress.setVisibility(View.VISIBLE);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        wvNewsDetails.getSettings().setJavaScriptEnabled(true);
        wvNewsDetails.getSettings().setAppCacheEnabled(true);
        //设置 缓存模式
        wvNewsDetails.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启 DOM storage API 功能
        wvNewsDetails.getSettings().setDomStorageEnabled(true);
        wvNewsDetails.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        });
        wvNewsDetails.loadUrl(extras.getString("URL"));
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_newsdetails;
    }

    @Override
    protected void initToolbar() {
        StatusBarCompatUtils.compat(this);
        ivToolbarLeft.setVisibility(View.VISIBLE);
        tvToolbarTitle.setVisibility(View.VISIBLE);
        tvToolbarTitle.setText("话题介绍");
    }


    @OnClick({R.id.iv_toolbar_left, R.id.tv_toolbar_title})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_toolbar_left:
                onBackPressed();
                break;
            case R.id.tv_toolbar_title:
                break;
        }
    }
}
